#########################################################################################
####################################  TABLE A4   ########################################
################  Global Assessment Survey Response Rates and Effect  ###################
#######################  of Conditions on Non-Response  #################################
#########################################################################################

rm(list=ls())

library(dplyr)
library(modelsummary)
library(RStata)
library(gt)
library(estimatr)

#load original data
##Set working directory
#setwd("C:/Users/ganth/Dropbox/StrengthInNumbersReplicationPackage/replicable/table_a4")
  
#########################################################################################
  
load(file = "table_a4_dataset_1.Rda")
  
  
perc_s1 <- format((sum(study1$tooksurvey_s1==1)/length(study1$tooksurvey_s1))*100, digits=4)
perc_s2 <- format((sum(study1$tooksurvey_s2==1)/length(study1$tooksurvey_s1))*100, digits=4)
perc_s3 <- format((sum(study1$tooksurvey_s3==1)/length(study1$tooksurvey_s1))*100, digits=4)
perc_s4 <- format((sum(study1$tooksurvey_s4==1)/length(study1$tooksurvey_s1))*100, digits=4)
perc_s5 <- format((sum(study1$tooksurvey_s5==1)/length(study1$tooksurvey_s1))*100, digits=4)

study1_newrow <- tribble(~term,          ~"Month 1",  ~"Month 2", ~"Month 3", ~"Month 4", ~"Month 5",
                         'Response Rate', perc_s1, perc_s2, perc_s3, perc_s4, perc_s5)
attr(study1_newrow, 'position') <- c(6)
rm(perc_s1, perc_s2, perc_s3, perc_s4, perc_s5)

study1_s1_robust <- lm_robust(tooksurvey_s1 ~ condition_1f,
                              clusters = group_id_fall,
                              se_type = 'stata',
                              data = study1)

study1_s2_robust <- lm_robust(tooksurvey_s2 ~ condition_1f,
                              clusters = group_id_fall,
                              se_type = 'stata',
                              data = study1)

study1_s3_robust <- lm_robust(tooksurvey_s3 ~ condition_1f,
                              clusters = group_id_fall,
                              se_type = 'stata',
                              data = study1)

study1_s4_robust <- lm_robust(tooksurvey_s4 ~ condition_1f,
                              clusters = group_id_fall,
                              se_type = 'stata',
                              data = study1)

study1_s5_robust <- lm_robust(tooksurvey_s5 ~ condition_1f,
                              clusters = group_id_fall,
                              se_type = 'stata',
                              data = study1)


#############
##Study 2
#############

load(file = "table_a4_dataset_2.Rda")

perc_s1 <- format((sum(study2$tooksurvey_s1==1)/length(study2$tooksurvey_s1))*100, digits=4)
perc_s2 <- format((sum(study2$tooksurvey_s2==1)/length(study2$tooksurvey_s1))*100, digits=4)
perc_s3 <- format((sum(study2$tooksurvey_s3==1)/length(study2$tooksurvey_s1))*100, digits=4)
perc_s4 <- format((sum(study2$tooksurvey_s4==1)/length(study2$tooksurvey_s1))*100, digits=4)
perc_s5 <- format((sum(study2$tooksurvey_s5==1)/length(study2$tooksurvey_s1))*100, digits=4)

study2_newrow <- tribble(~term,          ~"Month 1",  ~"Month 2", ~"Month 3", ~"Month 4", ~"Month 5",
                         'Response Rate', perc_s1, perc_s2, perc_s3, perc_s4, perc_s5)
attr(study2_newrow, 'position') <- c(6)
rm(perc_s1, perc_s2, perc_s3, perc_s4, perc_s5)

study2_s1_robust <- lm_robust(tooksurvey_s1 ~ condition_2f,
                              clusters = group_id,
                              se_type = 'stata',
                              data = study2)

study2_s2_robust <- lm_robust(tooksurvey_s2 ~ condition_2f,
                              clusters = group_id,
                              se_type = 'stata',
                              data = study2)

study2_s3_robust <- lm_robust(tooksurvey_s3 ~ condition_2f,
                              clusters = group_id,
                              se_type = 'stata',
                              data = study2)

study2_s4_robust <- lm_robust(tooksurvey_s4 ~ condition_2f,
                              clusters = group_id,
                              se_type = 'stata',
                              data = study2)

study2_s5_robust <- lm_robust(tooksurvey_s5 ~ condition_2f,
                              clusters = group_id,
                              se_type = 'stata',
                              data = study2)


##Combine into one table

studies <- list(
  "Study 1" = list("Month 1" = study1_s1_robust, "Month 2" = study1_s2_robust, "Month 3" = study1_s3_robust, "Month 4" = study1_s4_robust, "Month 5" = study1_s5_robust),
  "Study 2" = list("Month 1" = study2_s1_robust, "Month 2" = study2_s2_robust, "Month 3" = study2_s3_robust, "Month 4" = study2_s4_robust, "Month 5" = study2_s5_robust)
)

studyboth_newrow <- bind_rows(study1_newrow, study2_newrow)
attr(studyboth_newrow, 'position') <- c(5, 12)

modelsummary(studies,
             shape = "rbind",
             output = "table_a4.tex",
             coef_map = c("condition_1f" = "Majority Male Groups",
                          "condition_2f" = "Majority Male Groups",
                          "(Intercept)" = "Constant"),
             gof_map=c("nobs", "adj.r.squared"),
             stars= c('*' =0.1, '**' = 0.05, '***'=0.01),
             #title="Response Rates and Effects of Conditions on Survey Non-Response",
             add_rows = studyboth_newrow,
             notes = "Note: Table presents individual-level OLS analysis of the effects of group composition on survey non-response. The dependent variable is a dichotomous indicator of whether or not the study participant completed at least some portion of the monthly survey. Standard errors are clustered by group and reported in parentheses. Significance levels are indicated by $*$ $<.1$, ** $<.05$,  *** $<.01$.")



